#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "led_drv.h"
#include "key_drv.h"
#include "systick.h"
#include "rtc_drv.h"
#include "delay.h"
#include "sensor_drv.h"
#include "lcd_drv.h"
#include "wifi_drv.h"
#include "temp_humi_drv.h"
#include "pm25_drv.h"
#include "battery_drv.h"
#include "RTT_Debug.h"
#include "wifi_app.h"
#include "battery_app.h"
#include "sensor_app.h"
#include "config.h"

extern AsciiFontDesc_t g_fontAscii8x16;
extern AsciiFontDesc_t g_fontAscii10x20;

void HmiTest(void)
{
	if (GetKeyValWithBlock() != KEY1_SHORT_PRESS)
	{
		return;
	}
	TurnOnScreen();
	char strBuf[20] = {0};
	
	LcdDrawString(20, 25, "This is B6 test page", WHITE, BLACK, &g_fontAscii10x20); 
	while (1)
	{
		
		BatteryTask();
		LcdDrawString(10, 70, "Charge =", WHITE, BLACK, &g_fontAscii8x16);
		if (GetBatteryState() == BATTERY_NOT_CHARGE)
		{
			LcdDrawString(80, 70, "NO ", RED, BLACK, &g_fontAscii8x16);
		}
		else
		{
			LcdDrawString(80, 70, "YES", GREEN, BLACK, &g_fontAscii8x16);
		}
		
		LcdDrawString(10, 90, "Battery Voltage = ", WHITE, BLACK, &g_fontAscii8x16);	
		sprintf(strBuf, "%.1fv", GetBatteryVolt() / 1000.0);
		LcdDrawString(150, 90, strBuf, GREEN, BLACK, &g_fontAscii8x16);	

		SensorTask();
		LcdDrawString(10, 110, "Temp = ", WHITE, BLACK, &g_fontAscii8x16);
		float tempData;
		if (GetTempData(&tempData))
		{
			memset(strBuf, 0, sizeof(strBuf));
			sprintf(strBuf, " %.1f", tempData);
			LcdDrawString(60, 110, strBuf, GREEN, BLACK, &g_fontAscii8x16);		
		}
		else
		{
			LcdDrawString(60, 110, "--", RED, BLACK, &g_fontAscii8x16);				
		}
		
		LcdDrawString(10, 130, "Humi = ", WHITE, BLACK, &g_fontAscii8x16);
		float humiData;
		if (GetHumiData(&humiData))
		{
			memset(strBuf, 0, sizeof(strBuf));
			sprintf(strBuf, " %.1f", humiData);
			LcdDrawString(60, 130, strBuf, GREEN, BLACK, &g_fontAscii8x16);		
		}
		else
		{
			LcdDrawString(60, 130, "--", RED, BLACK, &g_fontAscii8x16);				
		}

		Pm25SensorData_t pm25SensorData;
		LcdDrawString(10, 150, "pm2.5 = ", WHITE, BLACK, &g_fontAscii8x16);
		if (GetPm25Data(&pm25SensorData))
		{
			memset(strBuf, 0, sizeof(strBuf));
			sprintf(strBuf, "%d", pm25SensorData.pm2p5Conc);
			LcdDrawString(70, 150, strBuf, GREEN, BLACK, &g_fontAscii8x16);		
		}
		else
		{
			LcdDrawString(70, 150, "--", RED, BLACK, &g_fontAscii8x16);				
		}
		
		
		
		LcdDrawString(10, 170, "Wi-Fi = ", WHITE, BLACK, &g_fontAscii8x16);
		WifiNetworkTask();
		if (GetWifiConnectState() != WIFI_CHECK_HARDWARE)
		{
			LcdDrawString(70, 170, "YES", GREEN, BLACK, &g_fontAscii8x16);		
		}
		else
		{
			LcdDrawString(70, 170, "NO", RED, BLACK, &g_fontAscii8x16);				
		}	

		LcdDrawString(10, 190, "Soft Version = ", WHITE, BLACK, &g_fontAscii8x16);
		LcdDrawString(130, 190, SOFT_VERSION, GREEN, BLACK, &g_fontAscii8x16);
		LcdDrawString(10, 210, "Hard Version = ", WHITE, BLACK, &g_fontAscii8x16);
		LcdDrawString(130, 210, HARD_VERSION, GREEN, BLACK, &g_fontAscii8x16);
	}	
}
